package org.video.mine.fragment.home;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.video.mine.HomeDocModel;
import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.adapter.TvOtherAdapter;
import org.video.mine.databinding.ForthFragmentBinding;
import org.video.mine.entity.DongHuaEntity;
import org.video.mine.network.JsoupGet;

import java.util.ArrayList;
import java.util.List;

public class ForthFragment extends Fragment {

    private ForthViewModel mViewModel;
    private ForthFragmentBinding binding;
    private ViewModelStoreOwner owner;
    private int page=1;
    private HomeDocModel homeDocModel;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    donghuaAdapter.notifyDataSetChanged();
                    break;
                case 1:
                    Toast.makeText(getContext(),"没有更多了",Toast.LENGTH_SHORT).show();
                    binding.srf.finishLoadMore();
                    break;
            }
        }
    };
    private TvOtherAdapter donghuaAdapter;

    public static ForthFragment newInstance() {
        return new ForthFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.forth_fragment,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        owner = this;
        mViewModel = new ViewModelProvider(this).get(ForthViewModel.class);
        homeDocModel = new ViewModelProvider(this).get(HomeDocModel.class);
        // TODO: Use the ViewModel
        if (homeDocModel.dongHuaEntities==null){
            homeDocModel.dongHuaEntities = new ArrayList<>();
        }
        initView();
    }

    private void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),1);
        binding.cyclerView.setLayoutManager(gridLayoutManager);
        donghuaAdapter = new TvOtherAdapter(homeDocModel.dongHuaEntities, getContext(), "donghua");
        binding.cyclerView.setAdapter(donghuaAdapter);
        initData();

        binding.srf.setEnableRefresh(false);
        binding.srf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (page>homeDocModel.zongyiPages){
                    page--;
                    mHandler.sendEmptyMessage(1);
                }else {
                    initData();
                }
            }
        });
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (homeDocModel.dongHuaEntities==null){
                    homeDocModel.dongHuaEntities = new ArrayList<>();
                }
                List<DongHuaEntity> dongHua = JsoupGet.getDongHua(owner, Urls.donghua, page);
                homeDocModel.dongHuaEntities.addAll(dongHua);
                mHandler.sendEmptyMessage(0);
                binding.srf.finishLoadMore();
            }
        }).start();
    }
}
